package thin4ca

class RequestHandler(tf: TemplateFactory, domain: Domain) {

  def handleRequest(templateName: String, args: String): String = {

    val t = tf.read(templateName)
    val definitions = t.analyze
    println("definitions " + definitions)
    
    var entityAttrs = Map[String, Any]()
    definitions.foreach(definition => {
      val mainEntity = definition.split("\\.")(0)
      val attr = definition.drop(mainEntity.length + 1)
      println("mainEntity " + mainEntity)
      println("attr " + attr)
      val value = domain.findAttr(mainEntity, args, attr)
      println("xxx " + value)
      entityAttrs = entityAttrs + ( (definition, value) )
    })
    
    println("entityAttrs " + entityAttrs)
    t.populate(entityAttrs)
  }
}
